package com.wangshaoyu.music_player.repository;

import com.wangshaoyu.music_player.data_object.AlbumDo;
import com.wangshaoyu.music_player.data_object.AlbumWithUserDo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface AlbumRepo {
    @Insert("insert into albums (uid, cover, title, state) values (#{uid}, #{cover}, #{title}, 1)")
    void insert(
            @Param("uid") int uid,
            @Param("title") String title,
            @Param("cover") String cover);

    @Select("select aid, title, cover, state from albums where uid = #{uid} order by state desc, aid desc")
    List<AlbumDo> selectListByUid(@Param("uid") int uid);

    @Select("select aid, uid, title, cover, state from albums where aid = #{aid}")
    AlbumDo selectOneByAid(@Param("aid") int aid);

    @Update("update albums set state = 2 where aid = #{aid}")
    void updateToPublishedByAid(@Param("aid") int aid);

    @Update("update albums set state = 0 where aid = #{aid}")
    void updateToWithdrawnByAid(@Param("aid") int aid);

    @Select("select aid, a.uid, username, title, cover from albums a join users u on a.uid = u.uid where state = 2 order by aid desc")
    List<AlbumWithUserDo> selectListAllWithUser();

    @Select("select aid, a.uid, username, title, cover from albums a join users u on a.uid = u.uid where aid = #{aid} and state = 2")
    AlbumWithUserDo selectOneWithUserByAid(@Param("aid") int aid);
}
